package com.erp.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.erp.dto.Apply;
import com.erp.dto.Examine;
import com.erp.dto.User;

@SuppressWarnings("serial")
@Controller(value = "examineAction")
@Scope(value = "prototype")
public class ExamineAction extends ModelDrivenBaseAction<Examine> {

	/**
	 * 审核页面
	 */
	public String addPage() {
		model.setAuditor((User) session.get("user"));
		model = examineService.setWatched(model);
		super.pushIntoValueStack(model);
		return "addPage";
	}

	public void add() {
		Apply apply = applyService.getById(model.getApply().getId());
		String procInstId = apply.getProcInstId();
		String taskId = workflowService.getCurrentTaskId(procInstId);
		model.setAuditor((User) session.get("user"));
		model.setExamineTime(new Date());
		model.setTaskId(taskId);
		// 保存审批信息
		examineService.add(apply, model);

		// 执行完成任务操作
		workflowService.completeTaskByProcInstId(procInstId);
		// 获取下一个执行者的姓名
		String auditorName = workflowService.getAssignee(procInstId);
		if(auditorName != null){
			// TODO 设置下一个审核者的审批意见为待查看
			User auditor = userService.getUserByName(auditorName);
			examineService.addNext(apply, auditor);
		} else {
			// TODO 更新apply在流程变量里状态
			
		}

		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("msg", "true");
		jsonMap.put("auditor", auditorName);
		super.writeJson(jsonMap);
	}

}
